package com.yqyd.hy.service.impl;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yqyd.common.core.redis.RedisCache;
import com.yqyd.hy.domain.HyManifest;
import com.yqyd.hy.mapper.HyManifestMapper;
import com.yqyd.hy.service.IHyManifestService;
import org.apache.commons.collections4.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户提交的货单Service业务层处理
 *
 * @author Dhq
 * @date 2025-06-23
 */
@Service
public class HyManifestServiceImpl implements IHyManifestService {

    // logger
    private static final Logger log = LoggerFactory.getLogger(HyManifestServiceImpl.class);

    @Autowired
    private HyManifestMapper hyManifestMapper;

    @Resource
    private RedisCache redisCache;


    private final static String DJY_API = "http://www.dianjiangyuan.com";
    /**
     * 物流信息
     */
    static String EXPRESS_API = DJY_API + "/prod-api/orderCommon/getLogicalDetailNew";

    /**
     * 查询用户提交的货单
     *
     * @param id 用户提交的货单主键
     * @return 用户提交的货单
     */
    @Override
    public HyManifest selectHyManifestById(Long id) {
        return hyManifestMapper.selectHyManifestById(id);
    }

    /**
     * 查询用户提交的货单列表
     *
     * @param hyManifest 用户提交的货单
     * @return 用户提交的货单
     */
    @Override
    public List<HyManifest> selectHyManifestList(HyManifest hyManifest) {
        return hyManifestMapper.selectHyManifestList(hyManifest);
    }

    /**
     * 新增用户提交的货单
     *
     * @param hyManifest 用户提交的货单
     * @return 结果
     */
    @Override
    public int insertHyManifest(HyManifest hyManifest) {
        return hyManifestMapper.insertHyManifest(hyManifest);
    }

    /**
     * 修改用户提交的货单
     *
     * @param hyManifest 用户提交的货单
     * @return 结果
     */
    @Override
    public int updateHyManifest(HyManifest hyManifest) {
        return hyManifestMapper.updateHyManifest(hyManifest);
    }

    /**
     * 批量删除用户提交的货单
     *
     * @param ids 需要删除的用户提交的货单主键
     * @return 结果
     */
    @Override
    public int deleteHyManifestByIds(Long[] ids) {
        return hyManifestMapper.deleteHyManifestByIds(ids);
    }

    /**
     * 删除用户提交的货单信息
     *
     * @param id 用户提交的货单主键
     * @return 结果
     */
    @Override
    public int deleteHyManifestById(Long id) {
        return hyManifestMapper.deleteHyManifestById(id);
    }

    @Override
    public Map<String,Object> SearchExpress(String number) {
        String authorization = redisCache.getCacheObject("DJY_token");
        if (Objects.isNull(authorization)) {
            throw new RuntimeException("未授权的接口");
        }
        Map<String, Object> paramsMap = new HashedMap<>();
        paramsMap.put("logisticNo", number);
        String result = HttpRequest.get(EXPRESS_API).header("Authorization", authorization)
                .header("accept-language", "zh-CN").form(paramsMap).execute().body();
        JSON json = JSONUtil.parse(result);
        if (!json.getByPath("code").toString().equals("200") ||
                Objects.isNull(json.getByPath("code"))) {
            throw new RuntimeException("授权的已过期");
        }
        JSONObject data = (JSONObject) json.getByPath("data");
        log.info(JSONUtil.toJsonStr(data));
        JSONArray jsonArray = data.getJSONArray("data");
        if(jsonArray.isEmpty()){
            return Map.of("state", "0", "recordList", jsonArray);
        }
        String state = data.getStr("state");
        jsonArray.sort(Comparator.comparingInt(a -> ((JSONObject) a).getInt("time")).reversed());
        return Map.of("state", state, "recordList", jsonArray);
    }
}
